<?php


namespace app\api\controller;
use app\api\logic\Posts as PostsLogic;
use app\api\logic\Merchant as MerchantLogic;

use think\App;
use think\Controller;
use think\facade\Session;

class Posts extends Controller
{
    protected $failException = true;
    private $postsLogic;
    private $merchantLogic;

    public function __construct(App $app = null,PostsLogic $postsLogic,MerchantLogic $merchantLogic)
    {
        parent::__construct($app);
        $this->postsLogic=$postsLogic;
        $this->merchantLogic=$merchantLogic;
    }

    public function info(){
        $data=$this->request->param();
        $this->validate($data,'app\api\validate\Posts.info');
        $info=$this->postsLogic->info($data['id']);
        if($info){
            return json(['data'=>$info,'code'=>ERRNO['OK'],'msg'=>'ok']);
        }else{
            return json(['code'=>ERRNO['POSTSERR'],'msg'=>'Posts not exist']);
        }
    }

    public function add(){
        $data=$this->request->param();
        $this->validate($data,'app\api\validate\Posts.add');
        $res=$this->postsLogic->add($data);
        if($res['code']==0){
            return json(['data'=>[],'code'=>ERRNO['OTHERERR'],'msg'=>$res['msg']]);
        }else{
            return json(['data'=>[],'code'=>ERRNO['OK'],'msg'=>'ok']);
        }
    }

    public function edit(){
        $data=$this->request->param();
        $this->validate($data,'app\api\validate\Posts.edit');
        $res=$this->postsLogic->edit($data);
        if($res['code']==0){
            return json(['data'=>[],'code'=>ERRNO['OTHERERR'],'msg'=>$res['msg']]);
        }else{
            return json(['data'=>[],'code'=>ERRNO['OK'],'msg'=>'ok']);
        }
    }

    
    public function delete(){
        $data=$this->request->param();
        $this->validate($data,'app\api\validate\Posts.delete');
        $this->postsLogic->delete($data['id']);
        return json(['data'=>[],'code'=>ERRNO['OK'],'msg'=>'ok']);
    }

    public function like(){
        $data=$this->request->param();
        $this->validate($data,'app\api\validate\Posts.like');
        $type=$this->postsLogic->like($data['id']);
        if($type==-1){
            return json(['data'=>['is_like'=>0],'code'=>ERRNO['OK'],'msg'=>'cancel success']);
        }else{
            return json(['data'=>['is_like'=>1],'code'=>ERRNO['OK'],'msg'=>'add success']);
        }
    }

}